var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();

Page({
  data: {
    bannerInfo: {

    },
    categoryFilter: false,
    catSonFilter:false,
    filterCategory: [],
    filterCatSon:[],
    goodsList: [],
    categoryId: 0,
    cateSongoryId:0,
    currentSortType: 'default',
    currentSortOrder: 'desc',
    categoryName:'',
    cateSonName:'',
    page: 1,
    size: 1000
  },
  getData: function (e) {
    let that = this;
    util.request(api.getGoodsByLabel, { goodsLabel: e }, 'GET').then(function (res) {
      that.setData({
        goodsList: res,
        bannerInfo: {
          name: e,
          img_url: 'https://www.cqjiahuijia.com/upload/xcximg/activback.png'
        }
      })
      // if (res.errno === 0) {
      //   that.setData({
      //     bannerInfo: res.data.bannerInfo,
      //   });
      //   that.getGoodsList();
      // }
    });
  },
  // getGoodsList() {
  //   var that = this;

  //   util.request(api.GoodsList, { isNew: 1, page: that.data.page, size: that.data.size, order: that.data.currentSortOrder, sort: that.data.currentSortType, categoryId: that.data.categoryId })
  //     .then(function (res) {
  //       if (res.errno === 0) {
  //         that.setData({
  //           goodsList: res.data.goodsList,
  //           filterCategory: res.data.filterCategory
  //         });
  //       }
  //     });
  // },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.getData(options.goodsLabel);
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  openSortFilter: function (event) {
    let that = this;
    let currentId = event.currentTarget.id;
    switch (currentId) {
      case 'categoryFilter':
        this.setData({
          'categoryFilter': !this.data.categoryFilter,
          'currentSortType': 'category',
          'currentSortOrder': 'asc'
        });
        util.request(api.getGoodsCat,'GET').then(function (res) {
          that.setData({
            filterCategory: res
          })
        })
        break;
      case 'priceSort':
        let tmpSortOrder = 'asc';
        if (this.data.currentSortOrder == 'asc') {
          tmpSortOrder = 'desc';
        }
        this.setData({
          'currentSortType': 'price',
          'currentSortOrder': tmpSortOrder,
          'categoryFilter': false
        });
        if (tmpSortOrder == 'asc'){
          let body={
            goodsLabel: that.data.bannerInfo.name,
            goodsCatName1: that.data.categoryName,
            goodsCatName2: that.data.cateSonName,
            sort: 'desc'
          }
          util.request(api.getGoodsByLabelPrcie, body, 'GET').then(function (res) {
          that.setData({
            goodsList: res,
          })
        })
        }else{
          let body = {
          goodsLabel: that.data.bannerInfo.name,
            goodsCatName1: that.data.categoryName,
            goodsCatName2: that.data.cateSonName,
          sort: 'asc'
        }
        util.request(api.getGoodsByLabelPrcie, body, 'GET').then(function (res) {
          that.setData({
            goodsList: res,
          })
        })
        }
 
        break;
      default:
        //综合排序
        this.getData(that.data.bannerInfo.name);
    }
  },
  selectCategory: function (event) {
    let that=this;
    let currentIndex = event.target.dataset.categoryIndex;
      let catsData = [];
      catsData.push({ 'catId': '全部', 'catName': '全部' });
    for (let i = 0; i < this.data.filterCategory.length; i++) {
      if (this.data.filterCategory[i].catId == event.currentTarget.dataset.id) {
        for (let j = 0; j < this.data.filterCategory[i].child.length; j++) {
          catsData.push(this.data.filterCategory[i].child[j]);
          }
          that.setData({
            filterCatSon: catsData,
            catSonFilter:true,
            categoryId: event.currentTarget.dataset.id,
            categoryName: event.currentTarget.dataset.name
          })
        }
      }
    },
    selectCatSon:function(event){
      let that=this;
    that.setData({
      cateSongoryId: event.currentTarget.dataset.id
    })
      let body={
        goodsLabel: that.data.bannerInfo.name,
        goodsCatName1: that.data.categoryName,
        goodsCatName2: event.currentTarget.dataset.name
      }
      util.request(api.getGoodsByLabelToCat, body, 'GET').then(function (res) {
        that.setData({
          goodsList: res,
          catSonFilter: false,
          categoryFilter: false,
          cateSonName: event.currentTarget.dataset.name
        })
      })
    }
  
})